home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 4 / ETO Development Tools 4.iso / Tools - Objects / MacApp / MacApp 3.0a2 / Libraries / USectionMgr.cp < prev    next >
Encoding:
Text File  |  1991-05-01  |  10.5 KB  |  450 lines  |  [TEXT/MPS ]

  1. // USectionMgr.cp
  2. // Copyright © 1984-1991 by Apple Computer Inc. All rights reserved.
  3.  
  4. #ifndef __USECTIONMGR__
  5. #include <USectionMgr.h>
  6. #endif
  7.  
  8. #ifndef __UFILE__
  9. #include <UFile.h>
  10. #endif
  11.  
  12. #ifndef __UAPPLEEVENTS__
  13. #include <UAppleEvents.h>
  14. #endif
  15.  
  16. #ifndef __UAPPLICATION__
  17. #include <UApplication.h>
  18. #endif
  19.  
  20. #ifndef __USECTION__
  21. #include <USection.h>
  22. #endif
  23.  
  24. #ifndef __UDOCUMENT__
  25. #include <UDocument.h>
  26. #endif
  27.  
  28. #ifndef __UPRINTHANDLER__
  29. #include <UPrintHandler.h>
  30. #endif
  31.  
  32. #ifndef __BALLOONS__
  33. #include <Balloons.h>
  34. #endif
  35.  
  36. #ifndef __UVIEW__
  37. #include <UView.h>
  38. #endif
  39.  
  40. #ifndef __UADORNERS__
  41. #include <UAdorners.h>
  42. #endif
  43.  
  44. #ifndef __UWINDOW__
  45. #include <UWindow.h>
  46. #endif
  47.  
  48. #ifndef __USCROLLER__
  49. #include <UScroller.h>
  50. #endif
  51.  
  52. #ifndef __UFILEBASEDDOCUMENT__
  53. #include <UFileBasedDocument.h>
  54. #endif
  55.  
  56. #ifndef __UEDITIONDOCUMENT__
  57. #include <UEditionDocument.h>
  58. #endif
  59.  
  60. #ifndef __UFAILURE__
  61. #include <UFailure.h>
  62. #endif
  63.  
  64. #ifndef __UMACAPPUTILITIES__
  65. #include <UMacAppUtilities.h>
  66. #endif
  67.  
  68. #ifndef __UERRORMGR__
  69. #include <UErrorMgr.h>
  70. #endif
  71.  
  72. #ifndef __UMEMORY__
  73. #include <UMemory.h>
  74. #endif
  75.  
  76. #ifndef __MENUS__
  77. #include <Menus.h>
  78. #endif
  79.  
  80. #ifndef __UMENUMGR__
  81. #include <UMenuMgr.h>
  82. #endif
  83.  
  84. #ifndef __UDESKSCRAPVIEW__
  85. #include <UDeskScrapView.h>
  86. #endif
  87.  
  88. #ifndef __UMACAPPGLOBALS__
  89. #include <UMacAppGlobals.h>
  90. #endif
  91.  
  92. #ifndef __ERRORS__
  93. #include <Errors.h>
  94. #endif
  95.  
  96. #ifndef __TOOLUTILS__
  97. #include <ToolUtils.h>
  98. #endif
  99.  
  100. #ifndef __UBUSYCURSOR__
  101. #include <UBusyCursor.h>
  102. #endif
  103.  
  104. #ifndef __UDEBUG__
  105. #include <UDebug.h>
  106. #endif
  107.  
  108. #ifndef __UINSPECTOR__
  109. #include <UInspector.h>
  110. #endif
  111.  
  112. #ifndef __SCRAP__
  113. #include <Scrap.h>
  114. #endif
  115.  
  116. #ifndef __UCLIPBOARDMGR__
  117. #include <UClipboardMgr.h>
  118. #endif
  119.  
  120. #ifndef __RESOURCES__
  121. #include <Resources.h>
  122. #endif
  123.  
  124. #ifndef __PACKAGES__
  125. #include <Packages.h>
  126. #endif
  127.  
  128. #ifndef __SCRIPT__
  129. #include <Script.h>
  130. #endif
  131.  
  132. #ifndef __STDIO__
  133. #include <StdIO.h>
  134. #endif
  135.  
  136. //--------------------------------------------------------------------------------------------------
  137. #pragma segment MAInit
  138.  
  139. pascal void TSectionMgr::ISectionMgr()
  140.  
  141. {
  142.     this->IBehavior();
  143. }
  144.  
  145. //--------------------------------------------------------------------------------------------------
  146. #pragma segment MAApplicationRes
  147.  
  148. pascal void TSectionMgr::DoAppleCommand(CmdNumber aCmdNumber,
  149.                                         const AppleEvent& message,
  150.                                         const AppleEvent& reply)
  151. {
  152.     TAppleCommand * theCommand = NULL;
  153.     VOLATILE(theCommand);                        // needed in failure handling below
  154.  
  155.     if (gConfiguration.hasEditionMgr)            // ensure it was initialized 
  156.         switch (aCmdNumber)
  157.         {
  158.             case cSectionRead:
  159.                 theCommand = new TSectionReadEventCmd;
  160.                 break;
  161.  
  162.             case cSectionWrite:
  163.                 theCommand = new TSectionWriteEventCmd;
  164.                 break;
  165.  
  166.             case cSectionScroll:
  167.                 theCommand = new TSectionScrollEventCmd;
  168.                 break;
  169.  
  170.             case cSectionCancel:
  171.                 theCommand = new TSectionCancelEventCmd;
  172.                 break;
  173.         }
  174.  
  175.     if (theCommand)
  176.     {
  177.         FailInfo fi;
  178.  
  179.         if (fi.Try())
  180.         {
  181.             theCommand->InitFromAppleEvent(aCmdNumber, NULL, NULL, message, reply);
  182.             fOwner->PostCommand(theCommand);
  183.  
  184.             fi.Success();
  185.         }
  186.         else    // Recover
  187.         {
  188.             theCommand = (TAppleCommand *)FreeIfObject(theCommand);
  189.  
  190.             fi.ReSignal();
  191.         }
  192.     }
  193.     else
  194.         inherited::DoAppleCommand(aCmdNumber, message, reply);
  195. }
  196.  
  197. //--------------------------------------------------------------------------------------------------
  198. #pragma segment MAFields
  199.  
  200. pascal void TSectionMgr::Fields(TObject* obj)    // override 
  201.  
  202. {
  203.     obj->DoToField("TSectionMgr", (Ptr)NULL, bClass);
  204.  
  205.     inherited::Fields(obj);
  206. }
  207.  
  208. //******************************************************************************************
  209. //       T S e c t i o n C o m m a n d
  210. //******************************************************************************************
  211. #pragma segment MACommandRes
  212.  
  213. pascal void TSectionCommand::ISectionCommand(CmdNumber itsCmdNumber,
  214.                                              TDocument* itsDocument,
  215.                                              TView* itsView,
  216.                                              TSection* itsSection)
  217.  
  218. {
  219.     this->IAppleCommand(itsCmdNumber, itsDocument, itsView);
  220.     fSection = itsSection;
  221.  
  222.     /* By default, the section commands *don't* cause changes to the document and are
  223.      *not* undoable. If the document changes (e.g. during a section read event) then the
  224.       document should set its change count accordingly to reflect the fact that its data
  225.       has changed, rather than having this command always default to causing changes. */
  226.     fCanUndo = FALSE;
  227.     fCausesChange = FALSE;
  228. }
  229.  
  230. //--------------------------------------------------------------------------------
  231. #pragma segment MACommandRes
  232.  
  233. pascal void TSectionCommand::InitFromAppleEvent(CmdNumber itsCmdNumber,
  234.                                                 TDocument* itsDocument,
  235.                                                 TView* itsView,
  236.                                                 const AppleEvent& itsMessage,
  237.                                                 const AppleEvent& itsReply)// override 
  238.  
  239. {
  240.     SectionHandle aSectionHandle;
  241.  
  242.     inherited::InitFromAppleEvent(itsCmdNumber, itsDocument, itsView, itsMessage, itsReply);
  243.  
  244.     // get the section handle 
  245.     fMessage->ReadSectionHandle(keyDirectObject, aSectionHandle);
  246.     if (qDebug && gIntenseDebugging)
  247.         fprintf(stderr, "SectionHandle from AppleEvent = %ld \n", (long)aSectionHandle);
  248.  
  249.     // map the section handle to a section object 
  250.     fSection = this->GetSectionObject(aSectionHandle);
  251.     FailNIL(fSection);
  252.     if (qDebug)
  253.         if (!VerboseIsObject(fSection))
  254.             ProgramBreak("the section object appears to be corrupt");
  255.  
  256.         // associate the command correctly 
  257.     fChangedObject = fSection->fDocument;
  258.     if (qDebug)
  259.         if (!VerboseIsObject(fChangedObject))
  260.             ProgramBreak("the section object's document appears to be corrupt");
  261.  
  262.         // ensure that the section is registered 
  263.     FailOSErr(fSection->Register());
  264. }
  265.  
  266. //--------------------------------------------------------------------------------------------------
  267. #pragma segment MAApplicationRes
  268.  
  269. pascal TSection* TSectionCommand::GetSectionObject(SectionHandle aSectionHandle)
  270. {
  271.     CDocumentIterator iter(gApplication);
  272.     TSection* aSection = NULL;
  273.     
  274.     for (TDocument* aDocument = iter.FirstDocument(); iter.More(); aDocument = iter.NextDocument())
  275.     {
  276.         aSection = ((TEditionDocument *)aDocument)->GetSectionObject(aSectionHandle);
  277.         if (aSection)
  278.             return aSection;                    // found a TSection, so return it
  279.     }
  280.  
  281.     return NULL;                                // no TSection found for this SectionHandle
  282. }
  283.  
  284. //******************************************************************************************
  285. //       T S e c t i o n R e a d E v e n t C m d
  286. //******************************************************************************************
  287. #pragma segment MACommandRes
  288.  
  289. pascal void TSectionReadEventCmd::ISectionReadEventCmd(CmdNumber itsCmdNumber,
  290.                                                        TDocument* itsDocument,
  291.                                                        TView* itsView,
  292.                                                        TSubscriber* itsSubscriber)
  293.  
  294. {
  295.     this->ISectionCommand(itsCmdNumber, itsDocument, itsView, itsSubscriber);
  296. }
  297.  
  298. //--------------------------------------------------------------------------------
  299. #pragma segment MADoCommand
  300.  
  301. pascal void TSectionReadEventCmd::DoIt(void)    // override 
  302.  
  303. {
  304.     FailInfo fi;
  305.     
  306.     if (fi.Try())
  307.     {
  308.         TSubscriber * aSubscriber = (TSubscriber *)fSection;
  309.     
  310.         if ((!aSubscriber->fDocument->fStopAllEditions) && (aSubscriber->fSectionHandle != NULL) && (aSubscriber->GetUpdateMode() == sumAutomatic))
  311.             aSubscriber->Subscribe();
  312.             
  313.         fi.Success();
  314.     }
  315.     else
  316.     {
  317.         this->ReportError(fi.error, fi.message);
  318.         fi.ReSignal();
  319.     }
  320. }
  321.  
  322. //******************************************************************************************
  323. //       T S e c t i o n W r i t e E v e n t C m d
  324. //******************************************************************************************
  325. #pragma segment MACommandRes
  326.  
  327. pascal void TSectionWriteEventCmd::ISectionWriteEventCmd(CmdNumber itsCmdNumber,
  328.                                                          TDocument* itsDocument,
  329.                                                          TView* itsView,
  330.                                                          TPublisher* itsPublisher)
  331.  
  332. {
  333.     this->ISectionCommand(itsCmdNumber, itsDocument, itsView, itsPublisher);
  334. }
  335.  
  336. //--------------------------------------------------------------------------------
  337. #pragma segment MADoCommand
  338.  
  339. pascal void TSectionWriteEventCmd::DoIt(void)    // override 
  340.  
  341. {
  342.     FailInfo fi;
  343.     
  344.     if (fi.Try())
  345.     {
  346.         ((TPublisher *)fSection)->Publish();
  347.             
  348.         fi.Success();
  349.     }
  350.     else
  351.     {
  352.         this->ReportError(fi.error, fi.message);
  353.         fi.ReSignal();
  354.     }
  355.  
  356. }
  357.  
  358. //******************************************************************************************
  359. //       T S e c t i o n S c r o l l E v e n t C m d
  360. //******************************************************************************************
  361. #pragma segment MACommandRes
  362.  
  363. pascal void TSectionScrollEventCmd::ISectionScrollEventCmd(CmdNumber itsCmdNumber,
  364.                                                            TDocument* itsDocument,
  365.                                                            TView* itsView,
  366.                                                            TSection* itsSection)
  367.  
  368. {
  369.     this->ISectionCommand(itsCmdNumber, itsDocument, itsView, itsSection);
  370. }
  371.  
  372. //--------------------------------------------------------------------------------
  373. #pragma segment MADoCommand
  374.  
  375. pascal void TSectionScrollEventCmd::DoIt(void)    // override 
  376.  
  377. {
  378.     // at this point, the application has received and processed the 'odoc' AppleEvent 
  379.     // so, the window containing the section should be frontmost
  380.  
  381.     FailInfo fi;
  382.     
  383.     if (fi.Try())
  384.     {
  385.         // 1. bring the application to front (using the Process Manager) 
  386.         gApplication->MakeFrontProcess();
  387.     
  388.         // 2. tell the document to reveal the selection and select it
  389.         if (fChangedObject)
  390.             ((TDocument*)fChangedObject)->RevealSelection(fSection->fDesignator);
  391.             
  392.         fi.Success();
  393.     }
  394.     else
  395.     {
  396.         this->ReportError(fi.error, fi.message);
  397.         fi.ReSignal();
  398.     }
  399. }
  400.  
  401. //******************************************************************************************
  402. //       T S e c t i o n C a n c e l E v e n t C m d
  403. //******************************************************************************************
  404. #pragma segment MACommandRes
  405.  
  406. pascal void TSectionCancelEventCmd::ISectionCancelEventCmd(CmdNumber itsCmdNumber,
  407.                                                            TDocument* itsDocument,
  408.                                                            TView* itsView,
  409.                                                            TSection* itsSection)
  410.  
  411. {
  412.     this->ISectionCommand(itsCmdNumber, itsDocument, itsView, itsSection);
  413. }
  414.  
  415. //--------------------------------------------------------------------------------
  416. #pragma segment MADoCommand
  417.  
  418. pascal void TSectionCancelEventCmd::DoIt(void)    // override 
  419.  
  420. {
  421.     FailInfo fi;
  422.     
  423.     if (fi.Try())
  424.     {
  425.         ((TEditionDocument *)fChangedObject)->CancelSection(fSection, kCancel);
  426.             
  427.         fi.Success();
  428.     }
  429.     else
  430.     {
  431.         this->ReportError(fi.error, fi.message);
  432.         fi.ReSignal();
  433.     }
  434. }
  435.  
  436. //******************************************************************************************
  437. //       G l o b a l     R o u t i n e s
  438. //******************************************************************************************
  439. #pragma segment MAInit
  440.  
  441. pascal void InitUSectionMgr(void)
  442.  
  443. {
  444.     TSectionMgr * aSectionMgr = new TSectionMgr;    // instantiate a section mgr 
  445.     aSectionMgr->ISectionMgr();                        // initialize it 
  446.     gApplication->AddBehavior(aSectionMgr);            // give the application this behavior
  447. }
  448.  
  449.  
  450.